/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AuctionsGUI.java
 *
 * Created on Oct 7, 2011, 8:03:27 PM
 */

package auctions;
import java.util.Vector;
import javax.swing.JFrame;
import org.dom4j.*;
import org.dom4j.io.*;
/**
 *
 * @author zxyang
 */
public class AuctionsGUI extends javax.swing.JFrame {

    private int k_para;
	private int n_para;
    private int nb_para;
    private String filename;
    private Vector<Integer> prs;
    private Vector<Double> rps;
    private Vector<Agent> agents;
    private Vector<AuctionResults> results;
    private AuctionHouse auctions;
    private javax.swing.JFrame frame;
    private int rpIndex;

    /** Creates new form AuctionsGUI */
    public AuctionsGUI() {
        k_para = 5;
        n_para = 2;
        nb_para = 10;
        rps = new Vector<Double>();
        prs = new Vector<Integer>();
        for(int i =0; i< 10; i++){
            rps.add(i, 0.0);
            prs.add(i,n_para);
        }

        filename="auction_results";
        results = new Vector<AuctionResults>();
        agents = new Vector<Agent>();
        rpIndex = 1;
        frame = new JFrame();
        initComponents();
        jPanel4.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        messageBox = new javax.swing.JOptionPane();
        jPanel3 = new javax.swing.JPanel();
        start = new javax.swing.JButton();
        createBidders = new javax.swing.JButton();
        createAuctions = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        k_Field = new javax.swing.JTextField();
        r_submit = new javax.swing.JButton();
        k_submit = new javax.swing.JButton();
        n_Label = new javax.swing.JLabel();
        k_Label = new javax.swing.JLabel();
        n_Field = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        nb_field = new javax.swing.JTextField();
        nb_submit = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultsArea = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        rpLabel = new javax.swing.JLabel();
        rpField = new javax.swing.JTextField();
        rp_submit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        start.setText("Start Bidding!");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        createBidders.setText("Create Bidders");
        createBidders.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createBiddersActionPerformed(evt);
            }
        });

        createAuctions.setText("Create Auctions");
        createAuctions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createAuctionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(createAuctions)
                .addGap(71, 71, 71)
                .addComponent(createBidders)
                .addGap(67, 67, 67)
                .addComponent(start)
                .addContainerGap(164, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createAuctions)
                    .addComponent(createBidders)
                    .addComponent(start))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        k_Field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                k_FieldActionPerformed(evt);
            }
        });

        r_submit.setText("submit");
        r_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                r_submitActionPerformed(evt);
            }
        });

        k_submit.setText("submit");
        k_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                k_submitActionPerformed(evt);
            }
        });

        n_Label.setText("Please enter payment rule number:");

        k_Label.setText("Please enter number of Simultaneous auctions:");

        jLabel1.setText("Please enter the number of bidders:");

        nb_submit.setText("submit");
        nb_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nb_submitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(n_Label)
                    .addComponent(k_Label)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(k_Field)
                    .addComponent(n_Field)
                    .addComponent(nb_field, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nb_submit)
                    .addComponent(r_submit)
                    .addComponent(k_submit))
                .addGap(191, 191, 191))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {k_Field, n_Field, nb_field});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {k_submit, nb_submit, r_submit});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(n_Label)
                    .addComponent(n_Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(r_submit))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nb_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nb_submit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(k_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(k_Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(k_submit)))
                .addContainerGap())
        );

        resultsArea.setColumns(20);
        resultsArea.setRows(5);
        jScrollPane1.setViewportView(resultsArea);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 734, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 710, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 332, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        rpLabel.setText("Please enter the nth auction's reserve price:");

        rp_submit.setText("submit");
        rp_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rp_submitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rpLabel)
                .addGap(61, 61, 61)
                .addComponent(rpField, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(rp_submit, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(205, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rpLabel)
                    .addComponent(rpField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rp_submit)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(339, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(228, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        auctions.setAgents(agents);// TODO add your handling code here:
        results = auctions.getWinner();
        String text = "";
        for (int i =1; i<= k_para; i++){
            AuctionResults ar = results.get(i-1);
            String auctionName = "Auction ";
            String name = auctionName.concat(Integer.toString(i));
            String winningPrice = Double.toString(ar.getWinningPrice());
            Vector<Double> bids = ar.getBids();
            String bidsname = "";
            for(int j=0; j< nb_para; j++){
                String bid = Double.toString(bids.get(j));
                int si = bid.indexOf('.');
                String nbid = bid.substring(0, si+3);
                System.out.println(nbid);
                bidsname=bidsname+nbid+", ";
                System.out.println(bidsname);
            }
            String winningBidder = Integer.toString(ar.getWinningBidder());
            
            String temp1 = name.concat(":"+"\nWinning Price: "+winningPrice+"\n:Bids:  "+bidsname+"\nWinning Bidder "+winningBidder+"\n\n");
            text =text.concat(temp1);

        }
        resultsArea.setText(text);
    }//GEN-LAST:event_startActionPerformed

    private void k_FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_k_FieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_k_FieldActionPerformed

    private void createBiddersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBiddersActionPerformed
        for (int i= 0; i< nb_para; i++){
            Agent agent = new DummyAgent(auctions);
            agents.add(agent);
        }

    }//GEN-LAST:event_createBiddersActionPerformed

    private void k_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_k_submitActionPerformed
       
        k_para = Integer.parseInt(k_Field.getText());
        String rpl1 = "Please enter the reserve price for auction: 1";
        rpLabel.setText(rpl1);
        jPanel4.setVisible(true);
    }//GEN-LAST:event_k_submitActionPerformed

    private void r_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_r_submitActionPerformed
        n_para = Integer.parseInt(n_Field.getText()); // TODO add your handling code here:
    }//GEN-LAST:event_r_submitActionPerformed

    private void rp_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rp_submitActionPerformed
        double rp = Double.parseDouble(rpField.getText());
        rps.add(rp);
        rpField.setText("");
        rpIndex++;
        if(rpIndex<=k_para){      
        String rpl1 = "Please enter the reserve price for auction:";
        String rpl = rpl1.concat(Integer.toString(rpIndex));
        rpLabel.setText(rpl);
        }
        else{
            rpIndex=1;
            jPanel4.setVisible(false);
        }
    }//GEN-LAST:event_rp_submitActionPerformed

    private void createAuctionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createAuctionsActionPerformed
       
            auctions = new AuctionHouseImpl(filename,nb_para,k_para,prs,rps);
 

            // TODO add your handling code here:
    }//GEN-LAST:event_createAuctionsActionPerformed

    private void nb_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nb_submitActionPerformed
        nb_para = Integer.parseInt(nb_field.getText()); // TODO add your handling code here:
    }//GEN-LAST:event_nb_submitActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AuctionsGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createAuctions;
    private javax.swing.JButton createBidders;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField k_Field;
    private javax.swing.JLabel k_Label;
    private javax.swing.JButton k_submit;
    private javax.swing.JOptionPane messageBox;
    private javax.swing.JTextField n_Field;
    private javax.swing.JLabel n_Label;
    private javax.swing.JTextField nb_field;
    private javax.swing.JButton nb_submit;
    private javax.swing.JButton r_submit;
    private javax.swing.JTextArea resultsArea;
    private javax.swing.JTextField rpField;
    private javax.swing.JLabel rpLabel;
    private javax.swing.JButton rp_submit;
    private javax.swing.JButton start;
    // End of variables declaration//GEN-END:variables

}
